package com.zh.view.data

import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import com.zh.mycommon.util.MyNetworkUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import com.zh.view.ChatMsg
import com.zh.view.FileDataItem
import com.zh.view.TaskStatus
import com.zh.view.component.CustomSnackbarVisuals
import com.zh.view.component.SnackbarType


// 1. 单例管理 Snackbar
object ElMessage {
    private var _snackbarState: SnackbarHostState? = null
    private var scope: CoroutineScope? = null

    val snackbarState: SnackbarHostState
        get() = _snackbarState ?: throw IllegalStateException("SnackbarState 未初始化")

    fun init(scope: CoroutineScope) {
        ElMessage.scope = scope
        _snackbarState = SnackbarHostState()
    }

    fun info(message: String) {
        scope?.launch {
            snackbarState.showSnackbar(message, duration = SnackbarDuration.Short)
        }
    }

    private fun show0(message: String, type: SnackbarType) {
        val visuals = CustomSnackbarVisuals(
            message = message,
            duration = SnackbarDuration.Short,
            type = type,
        )
        scope?.launch {
            snackbarState.showSnackbar(visuals)
        }
    }

    fun success(message: String) {
        show0(message, SnackbarType.SUCCESS)
    }

    fun warn(message: String) {
        show0(message, SnackbarType.WARN)
    }

    fun error(message: String) {
        show0(message, SnackbarType.ERROR)
    }
}

object AppStore {
    var localIp = mutableStateOf<String>(MyNetworkUtil.getHostIp())
    var peerIp = mutableStateOf<String>("")

    var dialogWindowId = mutableStateOf<String>("")
}

// 单例 Store
object SendTaskStore {
    val taskList = mutableStateListOf<FileDataItem>()

    fun addItem(item: FileDataItem) {
        taskList.add(item)
    }

    fun setProgressValueById(value: Float, id: String) {
        val item = taskList.find { it.id == id }
        if (item != null) {
            item.progressValue.value = value
            if (value < 1.0f) {
                item.status.value = TaskStatus.RUNNING
            } else {
                item.status.value = TaskStatus.COMPLETED
            }
        }
    }

    fun removeItemById(itemId: String): Boolean {
        return taskList.removeIf { it.id == itemId }
    }

    fun removeItem(item: FileDataItem): Boolean {
        return taskList.remove(item)
    }

    fun removeAll() {
        taskList.clear()
    }

    fun allChangeStatus(status: TaskStatus) {
        taskList.forEach {
            it.status.value = status
        }
    }
}

// 单例 Store
object ReceiveTaskStore {
    val taskList = mutableStateListOf<FileDataItem>()

    fun addItem(item: FileDataItem) {
        taskList.add(item)
    }

    fun setProgressValueById(value: Float, id: String) {
        val item = taskList.find { it.id == id }
        if (item != null) {
            item.progressValue.value = value
            if (value < 1.0f) {
                item.status.value = TaskStatus.RUNNING
            } else {
                item.status.value = TaskStatus.COMPLETED
            }
        }
    }

    fun removeItemById(itemId: String): Boolean {
        return taskList.removeIf { it.id == itemId }
    }

    fun removeItem(item: FileDataItem): Boolean {
        return taskList.remove(item)
    }

    fun removeAll() {
        taskList.clear()
    }
}

object ChatStore {
    private val messageList = mutableStateListOf<ChatMsg>()

    fun messages(): SnapshotStateList<ChatMsg> = messageList

    /**
     * 根据Id，查询ChatMsg
     * @returns {Promise<*>}
     * @param id
     */
    @Synchronized
    fun getById(id: String): ChatMsg? {
        return messageList.find { it.id == id }
    }

    /**
     * 添加到列表
     * @return true表示添加成功，false表示已经存在了，不再添加
     */
    @Synchronized
    fun addToList(msg: ChatMsg): Boolean {
        //去重复
        if (getById(msg.id) == null) {
            messageList.addLast(msg)
            return true
        } else {
            return false
        }
    }

    fun removeAll() {
        messageList.clear()
    }

}